package com.shaoshaohuo.app.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TabHost;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.adapter.MyPriceCommitedAdapter;
import com.shaoshaohuo.app.adapter.MyPriceReceivedAdapter;
import com.shaoshaohuo.app.adapter.MyPriceWatingAdapter;
import com.shaoshaohuo.app.constant.ExtraName;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.entity.MyPriceEntity;
import com.shaoshaohuo.app.net.HttpRequest;
import com.shaoshaohuo.app.net.RequestService;
import com.shaoshaohuo.app.ui.ec.QuoteDetailActivity;
import com.shaoshaohuo.app.ui.ec.QuoteListActivity;
import com.shaoshaohuo.app.ui.ec.SupplyOrderDetailActivity;
import com.shaoshaohuo.app.utils.ViewHelper;
import com.shaoshaohuo.app.view.TopbarView;
import com.shaoshaohuo.app.view.XListView;

import java.util.ArrayList;
import java.util.List;

/**
 *  我的报价
 * Created by cao on 2016/1/27.
 */
public class MyPriceActivity extends BaseActivity implements TabHost.OnTabChangeListener, XListView.IXListViewListener {

    private static final String TAB_ONE = "TAB_ONE";
    private static final String TAB_TWO = "TAB_TWO";
    private static final String TAB_THREE = "TAB_THREE";

    public static final int PRICE_WAIT = 1;
    public static final int PRICE_ALREADY = 2;
    public static final int PRICE_RECEIVE = 3;

    public int type = PRICE_WAIT;
    public int[] alreadyType = {PRICE_WAIT, 0, 0};

    private TopbarView topBar;
    private TabHost tabHost;

    private XListView listViewWating;
    private XListView listViewCommited;
    private XListView listViewReceived;

    private List<MyPriceEntity.MyPrice> listWating = new ArrayList<>();
    private List<MyPriceEntity.MyPrice> listCommited = new ArrayList<>();
    private List<MyPriceEntity.MyPrice> listReceived = new ArrayList<>();

    private MyPriceWatingAdapter adapterWating;
    private MyPriceCommitedAdapter adapterComited;
    private MyPriceReceivedAdapter adapterReceived;


    private String cursor = "";
    private String offset = "10";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_my_price);

        initView();
        setUpView();
        initData(true);
    }

    private void initView() {
        topBar = (TopbarView) findViewById(R.id.topbar);
        topBar.setCenterText("我的报价");
        topBar.setLeftView(true, true);

        tabHost = (TabHost) findViewById(android.R.id.tabhost);
        tabHost.setup();
        TabHost.TabSpec tab1 = tabHost.newTabSpec(TAB_ONE);
        tab1.setIndicator("待报价");
        tab1.setContent(R.id.linearLayout_waiting_price);
        tabHost.addTab(tab1);
        TabHost.TabSpec tab2 = tabHost.newTabSpec(TAB_TWO);
        tab2.setIndicator("已提交报价");
        tab2.setContent(R.id.linearLayout_commited_price);
        tabHost.addTab(tab2);
        TabHost.TabSpec tab3 = tabHost.newTabSpec(TAB_THREE);
        tab3.setIndicator("已收到报价");
        tab3.setContent(R.id.linearLayout_received_price);
        tabHost.addTab(tab3);

        ViewHelper.setTabHostStyle(tabHost.getTabWidget(), this, 0);

        listViewWating = (XListView) findViewById(R.id.listview_warting_price);
        listViewCommited = (XListView) findViewById(R.id.listview_commited_price);
        listViewReceived = (XListView) findViewById(R.id.listview_received_price);
    }


    private void setUpView() {
        listViewWating.setPullLoadEnable(true);
        listViewWating.setPullRefreshEnable(true);
        listViewCommited.setPullLoadEnable(true);
        listViewCommited.setPullRefreshEnable(true);
        listViewReceived.setPullLoadEnable(true);
        listViewReceived.setPullRefreshEnable(true);

        tabHost.setOnTabChangedListener(this);
        listViewWating.setXListViewListener(this);
        listViewCommited.setXListViewListener(this);
        listViewReceived.setXListViewListener(this);

        listViewWating.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //1  待报价
                Intent intent = new Intent(MyPriceActivity.this, GrabOrderActivity.class);
                intent.putExtra(ExtraName.orderid, listWating.get(position-1).orderid);
                startActivity(intent);
            }
        });

        listViewCommited.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int itemType = adapterComited.getItemViewType(position-1);
                if (itemType == 0) {//2司机对货主已经报价的订单
                    Intent intent = new Intent(MyPriceActivity.this, GrabOrderActivity.class);
                    intent.putExtra(ExtraName.orderid, listCommited.get(position-1).orderid);
                    intent.putExtra(ExtraName.id,listCommited.get(position-1).id);
                    startActivity(intent);
                }else if (itemType == 1) {//3 货主对买家已报价的订单
                    Intent intent = new Intent(MyPriceActivity.this, SupplyOrderDetailActivity.class);
                    intent.putExtra(ExtraName.orderid, listCommited.get(position-1).orderid);
                    startActivity(intent);
                }
            }
        });

        listViewReceived.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int itemType = adapterReceived.getItemViewType(position-1);
                if(itemType == 0){//4 供应收到司机报价
                    Intent intent = new Intent(MyPriceActivity.this, GrabOrderActivity.class);
                    intent.putExtra(ExtraName.orderid, listReceived.get(position-1).orderid);
                    intent.putExtra(ExtraName.showNumPrice, true);
                    startActivity(intent);
                }else if (itemType == 1) {//5 采购收到供应的报价
//                    Intent intent = new Intent(MyPriceActivity.this,QuoteListActivity.class);
//                    intent.putExtra(ExtraName.id, listReceived.get(position).orderid);
//                    startActivity(intent);
                    Intent intent = new Intent(MyPriceActivity.this,QuoteDetailActivity.class);
                    intent.putExtra(ExtraName.id, listReceived.get(position-1).id);
                    intent.putExtra(ExtraName.orderid, listReceived.get(position-1).orderid);
                    startActivity(intent);
                }
            }
        });

    }

    private void initData(final boolean isLoading) {
        if (isLoading) {
            startLoadingDialog();
        }
        RequestService.getInstance().myPrice(this, type, cursor, offset, MyPriceEntity.class, new HttpRequest.RequestListener() {
            @Override
            public void onSuccess(int requestCode, BaseEntity resultData) {
                dismissLoadingDialog();
                if (resultData.isOk()) {

                    MyPriceEntity entity = (MyPriceEntity) resultData;
                    if (entity.data != null && entity.data.size() > 0) {

                        setData(entity.data);
                    } else {
                        switch (type) {
                            case PRICE_WAIT:
                                listViewWating.endLoadingMore("没有更多了");
                                break;
                            case PRICE_ALREADY:
                                listViewCommited.endLoadingMore("没有更多了");
                                break;
                            case PRICE_RECEIVE:
                                listViewReceived.endLoadingMore("没有更多了");
                                break;
                        }
                    }
                } else {
                    showToast(resultData.getMsg());
                }
                onLoad();
            }

            @Override
            public void onFailed(int requestCode, Exception error, String msg) {
                dismissLoadingDialog();
                showToast(msg);
                onLoad();
            }
        });
    }


    private void setData(List<MyPriceEntity.MyPrice> list) {
        switch (type) {
            case PRICE_WAIT:
                listWating.addAll(list);
                if (adapterWating == null) {
                    adapterWating = new MyPriceWatingAdapter(this, listWating, true);
                    listViewWating.setAdapter(adapterWating);
                } else {
                    adapterWating.setData(listWating);
                }
                break;
            case PRICE_ALREADY:
                listCommited.addAll(list);
                if (adapterComited == null) {
                    adapterComited = new MyPriceCommitedAdapter(this, listCommited, true);
                    listViewCommited.setAdapter(adapterComited);
                } else {
                    adapterComited.setData(listCommited);
                }
                break;
            case PRICE_RECEIVE:
                listReceived.addAll(list);
                if (adapterReceived == null) {
                    adapterReceived = new MyPriceReceivedAdapter(this, listReceived, true);
                    listViewReceived.setAdapter(adapterReceived);
                } else {
                    adapterReceived.setData(listReceived);
                }
                break;
        }
    }

    private void onLoad() {
        switch (type) {
            case PRICE_WAIT:
                listViewWating.stopLoadMore();
                listViewWating.stopRefresh();
                break;
            case PRICE_ALREADY:
                listViewCommited.stopLoadMore();
                listViewCommited.stopRefresh();
                break;
            case PRICE_RECEIVE:
                listViewReceived.stopLoadMore();
                listViewReceived.stopRefresh();
                break;
        }
    }


    @Override
    public void onRefresh() {
        switch (type) {
            case PRICE_WAIT:
                listWating.clear();
                listViewWating.setPullLoadEnable(true);
                break;
            case PRICE_ALREADY:
                listCommited.clear();
                listViewCommited.setPullLoadEnable(true);
                break;
            case PRICE_RECEIVE:
                listReceived.clear();
                listViewReceived.setPullLoadEnable(true);
                break;
        }
        cursor = "";
        initData(false);
    }

    @Override
    public void onLoadMore() {
        switch (type) {
            case PRICE_WAIT:
                if (listWating != null && listWating.size() > 0) {
                    cursor = listWating.get(listWating.size() - 1).ctime;
                } else cursor = "";
                break;
            case PRICE_ALREADY:
                if (listCommited != null && listCommited.size() > 0) {
                    cursor = listCommited.get(listCommited.size() - 1).ctime;
                } else cursor = "";
                break;
            case PRICE_RECEIVE:
                if (listReceived != null && listReceived.size() > 0) {
                    cursor = listReceived.get(listReceived.size() - 1).ctime;
                } else cursor = "";
                break;
        }
        initData(false);
    }

    @Override
    public void onTabChanged(String tabId) {
        ViewHelper.setTabHostStyle(tabHost.getTabWidget(), this, tabHost.getCurrentTab());
        if (TAB_ONE.equals(tabId)) {
            type = PRICE_WAIT;
        } else if (TAB_TWO.equals(tabId)) {
            type = PRICE_ALREADY;
            if (alreadyType[1] == 0) {
                alreadyType[1] = type;
                initData(true);
            }
        } else if (TAB_THREE.equals(tabId)) {
            type = PRICE_RECEIVE;
            if (alreadyType[2] == 0) {
                alreadyType[2] = type;
                initData(true);
            }
        }

    }
}
